﻿// Author: Iulian Lita
// Project: UIModule
// Path: C:\Users\Tym\Documents\Visual Studio 2008\Projects\Library\UIModule
// Creation date: 1/13/2009 2:46 PM
// Last modified: 1/14/2009 3:02 AM

#region Using directives
using DatabaseModule;
using Error;
using System;
using System.Windows.Forms;
using UsersModule.AdminActions;
using ValidationModule;
using UsersModule;
using UsersModule.UserActions;
using UsersModule.AdminActions;
using UIModule;
#endregion


namespace ProjectIp
{
    /// <summary>
    /// Authentification form
    /// </summary>
    public partial class AuthentificationForm : Form
    {

        /// <summary>
        /// Create authentification form
        /// </summary>
        public AuthentificationForm()
        {
            InitializeComponent();
        } // AuthentificationForm()

        /// <summary>
        /// Form 1_ load
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">E</param>
        private void Form1_Load(object sender, EventArgs e)
        {
            createAccount_gbox.Visible = false;
            authentification_gbox.BringToFront();
        } // Form1_Load(sender, e)

        /// <summary>
        /// Create account _lklabel link clicked
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">E</param>
        private void createAccount_lklabel_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            createAccount_gbox.Visible = true;
            //authentification_gbox.Visible = false;
            createAccount_gbox.BringToFront();
             //createAccount_gbox.Location.x
        } // createAccount_lklabel_LinkClicked(sender, e)

        #region Create Account
        /// <summary>
        /// Create account _btn click
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">E</param>
        private void createAccount_btn_Click(object sender, EventArgs e)
        {
            //CreateAccount(user,password,userType);

            try
            {
                if (!FieldValidator.ValidUsername(cUser_tbox.Text))
                {
                    MessageBox.Show("Invalid username, please insert a different username");
                    return;
                } // if ()
                if (!FieldValidator.ValidName(firstName_tbox.Text))
                {
                    MessageBox.Show("Invalid first name, please insert a different first name");
                    return;
                } // if ()

                if (!FieldValidator.ValidName(lastName_tbox.Text))
                {
                    MessageBox.Show("Invalid last name, please insert a different last name");
                    return;
                } // if ()

                if (!FieldValidator.ValidEmail(email_tbox.Text))
                {
                    MessageBox.Show("Invalid email, please insert a different email");
                    return;
                } // if ()

                //if (!FieldValidator.ValidCNP(cnp_tbox.Text))
                //{
                //    MessageBox.Show("Invalid cnp, please insert a correct cnp");
                //    return;
                //}

                if (!FieldValidator.ValidPassword(cPassword_tbox.Text))
                {
                    MessageBox.Show("Invalid password, please insert a correct password");
                    return;
                } // if ()

                DatabaseWork databaseConnection = new DatabaseWork();
                databaseConnection.AddUser(lastName_tbox.Text, 
                    firstName_tbox.Text, 
                    cnp_tbox.Text, 
                    email_tbox.Text, 
                    cUser_tbox.Text, 
                    cPassword_tbox.Text, 
                    userType_cbox.SelectedIndex + 2, 
                    DateTime.Now.ToLocalTime().ToString());
                databaseConnection.Dispose();
            } // try
            catch (Exception exc)
            {
                ErrorManagement.ManageException(exc);
                MessageBox.Show("The user already exists or there is a database errorS");
                return;
            } // catch (exc)
            createAccount_gbox.Visible = false;
        } // createAccount_btn_Click(sender, e)
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            Application.Exit();
        } // button1_Click(sender, e)

        #region Login
        private void authentification_btn_Click(object sender, EventArgs e)
        {

            //Autentification(user,password);
            //authentification_gbox.Visible = false;

            if (!FieldValidator.ValidName(user_tbox.Text))
            {
                //Add error message
                return;
            } // if ()
            if (!FieldValidator.ValidPassword(password_tbox.Text))
            {
                //Add error message
                return;
            } // if ()

            DatabaseWork databaseConnection = new DatabaseWork();

            string userValues = databaseConnection.Login(user_tbox.Text, password_tbox.Text);

            GenericUser currentUser = UserFactory.GetUser(userValues, user_tbox.Text);

            databaseConnection.Dispose();

            //The 
            if (null == currentUser)
            {
                DefaultAdminActions adminActions = new DefaultAdminActions();
                AdministratorForm administrator_form = new AdministratorForm(adminActions);
                administrator_form.ParentForm = this;
                administrator_form.Show();
                this.Hide();
            } // if (userValues)    
            else
                if (currentUser.UserType == UserTypes.PUBLISHER)
                {
                    PublisherActions userActions = new PublisherActions((Publisher)currentUser);
                    PublisherForm publisher_form = new PublisherForm(userActions);
                    publisher_form.ParentForm = this;
                    publisher_form.Show();
                    this.Hide();
                }
                else
                {
                    DefaultUserActions userActions = new DefaultUserActions((User)currentUser);
                    UsersForms user_form = new UsersForms(userActions);
                    user_form.ParentForm = this;
                    user_form.Show();
                    this.Hide();
                }

        }
        #endregion

        private void authentification_gbox_Enter(object sender, EventArgs e)
        {

        } // authentification_btn_Click(sender, e)

        
    } // class AuthentificationForm
} // namespace ProjectIp
